package com.pz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.pz.gulimall.product.entity.BrandEntity;
import com.pz.gulimall.product.service.impl.CategoryBrandRelationServiceImpl;
import com.pz.gulimall.product.vo.res.BrandResVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.pz.gulimall.product.service.CategoryBrandRelationService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-01 20:22:28
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    private CategoryBrandRelationServiceImpl cateBrandServiceImpl;

    @GetMapping("/brands/list")
    public R getCateRelationBrands(@RequestParam(value = "catId", required = false) Long catId){
        List<BrandEntity> vos = categoryBrandRelationService.getByCatId(catId);
        List<BrandResVo> list = vos.stream().map(item -> {
            BrandResVo brandResVo = new BrandResVo();
            brandResVo.setBrandId(item.getBrandId());
            brandResVo.setBrandName(item.getName());
            return brandResVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list/{brandId}")
    public R listByBrandID(@PathVariable("brandId") Long brandId){
        List<CategoryBrandRelationEntity> res = cateBrandServiceImpl.getByBrandId(brandId);
        return R.ok().put("data", res);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
